<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>Add the Stackless Coroutines for tbox</title>
    <meta name="description" content="tbox provides a lightweight implementation of stackless coroutinesand it’s interfaces are very simple too, for example:tb_lo_coroutine_enter(coroutine){    w...">

    
    <meta name="keywords" content="tbox,stackless,coroutines,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/2016/12/03/stackless-coroutine/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/cn?lang=0">
                    
                        <i class="fa fa-home"></i>中文
                    </a>
                </li>

                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>Projects
                        </a>
                    </li>
                    
                    
                    
                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="/archive/">
                            
                        
                            <i class="fa fa-archive"></i>Archives
                        </a>
                    </li>
                    
                    
                    
                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="/category/">
                            
                        
                            <i class="fa fa-th-list"></i>Categories
                        </a>
                    </li>
                    
                    
                    
                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="/tag/">
                            
                        
                            <i class="fa fa-tags"></i>Tags
                        </a>
                    </li>
                    
                    
                    
                
                    
                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="/docs/">
                            
                        
                            <i class="fa fa-book"></i>Documents
                        </a>
                    </li>
                    
                    
                    
                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="https://xmake.io/#/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>Community
                        </a>
                    </li>
                    
                    
                    
                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="/donation/">
                            
                        
                            <i class="fa fa-heart"></i>Donate
                        </a>
                    </li>
                    
                    
                    
                
                    
                     
                    
                
                    
                     
                     
                    <li>

                        
                            
                            <a href="/about/">
                            
                        
                            <i class="fa fa-user"></i>About
                        </a>
                    </li>
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                     
                     
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>Add the Stackless Coroutines for tbox</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-12-03
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#tbox" title="Category: tbox" rel="category">tbox</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a-->
        <a href="/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a>&nbsp;
    
        <!--a href="/tag/#stackless" title="Tag: stackless" rel="tag">stackless</a-->
        <a href="/tag/#stackless" title="Tag: stackless" rel="tag">stackless</a>&nbsp;
    
        <!--a href="/tag/#coroutines" title="Tag: coroutines" rel="tag">coroutines</a-->
        <a href="/tag/#coroutines" title="Tag: coroutines" rel="tag">coroutines</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p><a href="https://github.com/waruqi/tbox">tbox</a> provides a lightweight implementation of stackless coroutines</p>

<p>and it’s interfaces are very simple too, for example:</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="n">tb_lo_coroutine_enter</span><span class="p">(</span><span class="n">coroutine</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">while</span> <span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">tb_lo_coroutine_yield</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p>The switch performance of this stackless coroutines is faster than the implementation of tbox’s <a href="/2016/10/29/coroutine-switch/">stackfull coroutines</a>.</p>

<p>And the memory storage space of each coroutine is also reduced to only a few bytes, but it also has many limitations:</p>

<div class="highlighter-rouge"><pre class="highlight"><code>1. With a stackless coroutine, only the top-level routine may be suspended. 
Any routine called by that top-level routine may not itself suspend. 
This prohibits providing suspend/resume operations in routines within a general-purpose library.

2. Because stackless coroutines do not save the stack context across a blocking call, local variables are not preserved when the protothread blocks.
This means that local variables should be used with utmost care - if in doubt, do not use local variables inside a stackless coroutine!
</code></pre>
</div>

<p>Now let’s look at how to use the stackless coroutine of tbox.</p>

<h4 id="switch">Switch</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">switchtask</span><span class="p">(</span><span class="n">tb_lo_coroutine_ref_t</span> <span class="n">coroutine</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// check
</span>    <span class="n">tb_size_t</span><span class="o">*</span> <span class="n">count</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_size_t</span><span class="o">*</span><span class="p">)</span><span class="n">priv</span><span class="p">;</span>

    <span class="c1">// enter coroutine
</span>    <span class="n">tb_lo_coroutine_enter</span><span class="p">(</span><span class="n">coroutine</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// loop
</span>        <span class="k">while</span> <span class="p">((</span><span class="o">*</span><span class="n">count</span><span class="p">)</span><span class="o">--</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="c1">// trace
</span>            <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"[coroutine: %p]: %lu"</span><span class="p">,</span> <span class="n">tb_lo_coroutine_self</span><span class="p">(),</span> <span class="o">*</span><span class="n">count</span><span class="p">);</span>

            <span class="c1">// yield
</span>            <span class="n">tb_lo_coroutine_yield</span><span class="p">();</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">test</span><span class="p">()</span>
<span class="p">{</span>
    <span class="c1">// init scheduler
</span>    <span class="n">tb_lo_scheduler_ref_t</span> <span class="n">scheduler</span> <span class="o">=</span> <span class="n">tb_lo_scheduler_init</span><span class="p">();</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">scheduler</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// start coroutines
</span>        <span class="n">tb_size_t</span> <span class="n">counts</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">};</span>
        <span class="n">tb_lo_coroutine_start</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">switchtask</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">counts</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">tb_null</span><span class="p">);</span>
        <span class="n">tb_lo_coroutine_start</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">switchtask</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">counts</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">tb_null</span><span class="p">);</span>

        <span class="c1">// run scheduler
</span>        <span class="n">tb_lo_scheduler_loop</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">tb_true</span><span class="p">);</span>

        <span class="c1">// exit scheduler
</span>        <span class="n">tb_lo_scheduler_exit</span><span class="p">(</span><span class="n">scheduler</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<h4 id="passing-arguments">Passing arguments</h4>

<p>You can pass the user private data and the custom free function before starting a new coroutine.</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="k">typedef</span> <span class="k">struct</span> <span class="n">__tb_xxxx_priv_t</span>
<span class="p">{</span>
    <span class="n">tb_size_t</span>   <span class="n">member</span><span class="p">;</span>
    <span class="n">tb_size_t</span>   <span class="n">others</span><span class="p">;</span>

<span class="p">}</span><span class="n">tb_xxxx_priv_t</span><span class="p">;</span>

<span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">tb_xxx_free</span><span class="p">(</span><span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">priv</span><span class="p">)</span> <span class="n">tb_free</span><span class="p">(</span><span class="n">priv</span><span class="p">);</span>
<span class="p">}</span>
 
<span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">test</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">tb_xxxx_priv_t</span><span class="o">*</span> <span class="n">priv</span> <span class="o">=</span> <span class="n">tb_malloc0_type</span><span class="p">(</span><span class="n">tb_xxxx_priv_t</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">priv</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">priv</span><span class="o">-&gt;</span><span class="n">member</span> <span class="o">=</span> <span class="n">value</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">tb_lo_coroutine_start</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">switchtask</span><span class="p">,</span> <span class="n">priv</span><span class="p">,</span> <span class="n">tb_xxx_free</span><span class="p">);</span>
<span class="p">}</span>
</code></pre>
</div>

<p>But this is very complicated to write, so we can use <code class="highlighter-rouge">tb_lo_coroutine_pass1</code> to simplify it:</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code> 
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">__tb_xxxx_priv_t</span>
<span class="p">{</span>
    <span class="n">tb_size_t</span>   <span class="n">member</span><span class="p">;</span>
    <span class="n">tb_size_t</span>   <span class="n">others</span><span class="p">;</span>

<span class="p">}</span><span class="n">tb_xxxx_priv_t</span><span class="p">;</span>

<span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">test</span><span class="p">()</span>
<span class="p">{</span>
    <span class="c1">// start coroutine 
</span>    <span class="n">tb_lo_coroutine_start</span><span class="p">(</span><span class="n">scheduler</span><span class="p">,</span> <span class="n">switchtask</span><span class="p">,</span> <span class="n">tb_lo_coroutine_pass1</span><span class="p">(</span><span class="n">tb_xxxx_priv_t</span><span class="p">,</span> <span class="n">member</span><span class="p">,</span> <span class="n">value</span><span class="p">));</span>
<span class="p">}</span>
</code></pre>
</div>

<p>This is functionally equivalent to the previous code.</p>

<h4 id="suspend-and-resume">Suspend and resume</h4>

<p>The usage of the two interfaces is same with tbox’s stackfull coroutine:</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="n">tb_lo_coroutine_enter</span><span class="p">(</span><span class="n">coroutine</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// suspend coroutine
</span>    <span class="n">tb_lo_coroutine_suspend</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">// resume the given coroutine
</span><span class="n">tb_lo_coroutine_resume</span><span class="p">(</span><span class="n">coroutine</span><span class="p">);</span>
</code></pre>
</div>

<p>The difference between suspend()/resume() and yield() is that the coroutine after yield is then switched back, 
but the suspended coroutine will never be executed until resumed by calling resume().</p>

<h4 id="sleep">Sleep</h4>

<p>We can simply use sleep() to wait some time:</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="n">tb_lo_coroutine_enter</span><span class="p">(</span><span class="n">coroutine</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// wait 1s
</span>    <span class="n">tb_lo_coroutine_sleep</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="p">}</span>
</code></pre>
</div>

<h4 id="wait-io">Wait io</h4>

<p>We can use <code class="highlighter-rouge">tb_lo_coroutine_waitio</code> to wait socket io events synchronously:</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code><span class="k">static</span> <span class="n">tb_void_t</span> <span class="nf">tb_demo_lo_coroutine_client</span><span class="p">(</span><span class="n">tb_lo_coroutine_ref_t</span> <span class="n">coroutine</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// check
</span>    <span class="n">tb_demo_lo_client_ref_t</span> <span class="n">client</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_demo_lo_client_ref_t</span><span class="p">)</span><span class="n">priv</span><span class="p">;</span>
    <span class="n">tb_assert</span><span class="p">(</span><span class="n">client</span><span class="p">);</span>

    <span class="c1">// enter coroutine
</span>    <span class="n">tb_lo_coroutine_enter</span><span class="p">(</span><span class="n">coroutine</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// read data
</span>        <span class="n">client</span><span class="o">-&gt;</span><span class="n">size</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">data</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span>
        <span class="k">while</span> <span class="p">(</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">read</span> <span class="o">&lt;</span> <span class="n">client</span><span class="o">-&gt;</span><span class="n">size</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="c1">// read it
</span>            <span class="n">client</span><span class="o">-&gt;</span><span class="n">real</span> <span class="o">=</span> <span class="n">tb_socket_recv</span><span class="p">(</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">sock</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_byte_t</span><span class="o">*</span><span class="p">)</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">data</span> <span class="o">+</span> <span class="n">client</span><span class="o">-&gt;</span><span class="n">read</span><span class="p">,</span> <span class="n">client</span><span class="o">-&gt;</span><span class="n">size</span> <span class="o">-</span> <span class="n">client</span><span class="o">-&gt;</span><span class="n">read</span><span class="p">);</span>

            <span class="c1">// has data?
</span>            <span class="k">if</span> <span class="p">(</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">real</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> 
            <span class="p">{</span>
                <span class="n">client</span><span class="o">-&gt;</span><span class="n">read</span> <span class="o">+=</span> <span class="n">client</span><span class="o">-&gt;</span><span class="n">real</span><span class="p">;</span>
                <span class="n">client</span><span class="o">-&gt;</span><span class="n">wait</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="c1">// no data? wait it
</span>            <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">real</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">wait</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="c1">// wait socket receiving events
</span>                <span class="n">tb_lo_coroutine_waitio</span><span class="p">(</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">sock</span><span class="p">,</span> <span class="n">TB_SOCKET_EVENT_RECV</span><span class="p">,</span> <span class="n">TB_DEMO_TIMEOUT</span><span class="p">);</span>

                <span class="c1">// get waited events
</span>                <span class="n">client</span><span class="o">-&gt;</span><span class="n">wait</span> <span class="o">=</span> <span class="n">tb_lo_coroutine_events</span><span class="p">();</span>
                <span class="n">tb_assert_and_check_break</span><span class="p">(</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">wait</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="c1">// failed or end?
</span>            <span class="k">else</span> <span class="k">break</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="c1">// trace
</span>        <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"echo: %s"</span><span class="p">,</span> <span class="n">client</span><span class="o">-&gt;</span><span class="n">data</span><span class="p">);</span>

        <span class="c1">// exit socket
</span>        <span class="n">tb_socket_exit</span><span class="p">(</span><span class="n">client</span><span class="o">-&gt;</span><span class="n">sock</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p>The scheduler of stackless coroutines will use a io poller (epoll, kqueue, poll ..) to schedule these waiting socket.</p>

<p>If you want to know more usage of the interfaces, please refer to the http server <a href="https://github.com/waruqi/tbox/blob/master/src/demo/coroutine/stackless/http_server.c">example</a> based on stackless coroutine.</p>

<p>Or more examples <a href="https://github.com/waruqi/tbox/tree/master/src/demo/coroutine/stackless">source codes</a>.</p>

<h4 id="semaphone-and-lock">Semaphone and lock</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>
<span class="c1">// the lock
</span><span class="k">static</span> <span class="n">tb_lo_lock_t</span>     <span class="n">g_lock</span><span class="p">;</span>

<span class="c1">// enter coroutine
</span><span class="n">tb_lo_coroutine_enter</span><span class="p">(</span><span class="n">coroutine</span><span class="p">)</span>
<span class="p">{</span>
    <span class="c1">// loop
</span>    <span class="k">while</span> <span class="p">(</span><span class="n">lock</span><span class="o">-&gt;</span><span class="n">count</span><span class="o">--</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// enter lock
</span>        <span class="n">tb_lo_lock_enter</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_lock</span><span class="p">);</span>

        <span class="c1">// trace
</span>        <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"[coroutine: %p]: enter"</span><span class="p">,</span> <span class="n">tb_lo_coroutine_self</span><span class="p">());</span>

        <span class="c1">// wait some time
</span>        <span class="n">tb_lo_coroutine_sleep</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>

        <span class="c1">// trace
</span>        <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"[coroutine: %p]: leave"</span><span class="p">,</span> <span class="n">tb_lo_coroutine_self</span><span class="p">());</span>

        <span class="c1">// leave lock
</span>        <span class="n">tb_lo_lock_leave</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_lock</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
 
<span class="c1">// init lock     
</span><span class="n">tb_lo_lock_init</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_lock</span><span class="p">);</span>

<span class="c1">// start coroutine 
// ..
</span>
<span class="c1">// exit lock
</span><span class="n">tb_lo_lock_exit</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_lock</span><span class="p">);</span>
</code></pre>
</div>


        </article>
        <hr>

        <!-- baidu ads -->
        

        
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
                    
                    <h2 id="chinese">中文</h2>
                    <ul>
                    
                    <li class="relatedPost">
                        <a href="/cn/2016/12/03/stackless-coroutine/">tbox新增stackless协程支持
                        
                        </a>
                    </li>
                    
                    
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
        
            </ul>
        

        
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        

        

        

        
        
        

        

        

        
        
        <p><strong>Prev Post</strong> <a href="/2016/11/15/syntax-style/">Two Syntax Styles of xmake</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        

        

        

        
        
        

        

        

        
        
        <p><strong>Next Post</strong> <a href="/2016/12/07/update-v1.6.1/">tbox v1.6.1 released, add stackless coroutine!</a></p>
        
    </div>
</div>


        <h2 id="comments">Comments</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,      
  language: 'en',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">


            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    Content
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#chinese">中文</a></li>
                    <li><a href="#similar_posts">Similar Posts</a></li>
                    <li><a href="#comments">Comments</a></li>
                </ul>
            </div>

            <!-- baidu ads -->
            

            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    Links
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

            <!-- chitika ads -->
            
        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
